package com.lsfb.duishangbaos.ShopManage;

import android.content.Context;
import android.net.Uri;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.lsfb.duishangbaos.R;
import com.lsfb.duishangbaos.Utils.MyCommonAdapter;
import com.lsfb.duishangbaos.Utils.URLString;
import com.zgscwjm.lsfbbasetemplate.Event.LsfbEvent;
import com.zgscwjm.lsfbbasetemplate.adapter.ViewHolder;
import com.zgscwjm.lsfbbasetemplate.utils.LsfbLog;

import java.util.List;

/**
 * Created by Administrator on 2017/5/15 0015.
 */

public class ShowAdapter extends MyCommonAdapter<ShopShowBean.ListBean> {

    private List<ShowViewBean> viewlist;

    public List<ShowViewBean> getViewlist() {
        return viewlist;
    }

    public void setViewlist(List<ShowViewBean> viewlist) {
        this.viewlist = viewlist;
    }

    public ShowAdapter(Context context, int layoutId, List<ShopShowBean.ListBean> list, List<ShowViewBean> viewlist) {
        super(context, layoutId, list);
        this.viewlist = viewlist;
    }

    @Override
    public void convert(final ViewHolder holder, final ShopShowBean.ListBean model) {
        viewlist.get(holder.getmPosition()).et_shop = holder.getView(R.id.item_et_import);
        viewlist.get(holder.getmPosition()).img_pic = holder.getView(R.id.item_img_sd);
        viewlist.get(holder.getmPosition()).item_img = holder.getView(R.id.item_img);
        viewlist.get(holder.getmPosition()).btn_del = holder.getView(R.id.item_btn_del);
        viewlist.get(holder.getmPosition()).img_add = holder.getView(R.id.item_btn_img);
        viewlist.get(holder.getmPosition()).img_del = holder.getView(R.id.item_img_del);
        viewlist.get(holder.getmPosition()).et_shop.setText(model.getBewrite());


        viewlist.get(holder.getmPosition()).et_shop.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                model.setBewrite(s.toString());
                LsfbLog.e("开始输入了");
            }

            @Override
            public void afterTextChanged(Editable s) {
                LsfbLog.e("开始输入了");
            }
        });
        if (model.getImg().isEmpty()) {
            viewlist.get(holder.getmPosition()).item_img.setVisibility(View.GONE);
        } else {
            viewlist.get(holder.getmPosition()).item_img.setVisibility(View.VISIBLE);
            viewlist.get(holder.getmPosition()).img_pic.setImageURI(Uri.parse(URLString.IMAGES + model.getImg()));
        }

        //上传图片
        viewlist.get(holder.getmPosition()).img_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowImgEvent event = new ShowImgEvent();
                event.setData(holder.getmPosition());
                LsfbEvent.getInstantiation().post(event);
            }
        });

        //删除图片
        viewlist.get(holder.getmPosition()).img_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowImgDelEvent event = new ShowImgDelEvent();
                event.setData(holder.getmPosition());
                LsfbEvent.getInstantiation().post(event);
            }
        });

        //删除item
        viewlist.get(holder.getmPosition()).btn_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowDelEvent event = new ShowDelEvent();
                event.setData(holder.getmPosition());
                LsfbEvent.getInstantiation().post(event);
            }
        });

    }
}
